﻿package 
{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.ByteArray;
	import org.aswing.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.geom.IntDimension;
	
	/**
	 * Reference http://www.aswing.org/?p=114
	 * @author ming
	 */
	public class Main extends Sprite 
	{
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			var frame : JFrame;
			
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			// entry point
			for (var i:int = 0; i < 2; ++i)
			{
				frame = new JFrame( this, "AsWingApplication" );
				frame.getContentPane().append( createCenterPane() );
				frame.setSize(new IntDimension( 200, 120 ) );
				frame.x = i * 200;
				frame.y = 10;
				frame.show();
			}
			
			{
				frame = new JFrame( this, "AsWingApplication" );
				var ta:JTextArea = new JTextArea();
				var sp:JScrollPane = new JScrollPane(ta); 
				frame.getContentPane().append( sp );
				frame.setSize(new IntDimension( 500, 360 ) );
				frame.x = 0;
				frame.y = 150;
				frame.show();
				
				var txtLoader : URLStream = new URLStream;
				
				var _ : Function = function (e : Event) : void
				{
					trace("Bible ready");
					//trace(txtLoader.data);
					
					var data : String = txtLoader.readUTFBytes(txtLoader.bytesAvailable);
					ta.setText(data);
					
					trace("setText done");
				}
				var onerr : Function = function (e:Event) : void
				{
					trace(e);
				}
				var onprogress : Function = function (e:ProgressEvent) : void
				{
					trace(e.bytesLoaded.toString() + "/" + e.bytesTotal.toString());
				}
				txtLoader.addEventListener(Event.COMPLETE, _);
				txtLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onerr);
				txtLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onerr);
				txtLoader.addEventListener(IOErrorEvent.IO_ERROR, onerr);
				txtLoader.addEventListener(ProgressEvent.PROGRESS, onprogress);
				
				
				//txtLoader.load(new URLRequest("http://www.o-bible.org/download/hb5.txt"));
				//txtLoader.load(new URLRequest("http://www.o-bible.org/download/bbe.txt"));
				txtLoader.load(new URLRequest("../hb_utf8.txt"));
				
			}
		}
		
		private function createCenterPane() : JPanel
		{
			var numClicks : int = 0;
			var labelPrefix : String = "Number of button clicks: ";
			var pane : JPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
			var label : JLabel = new JLabel(labelPrefix + "0");
			var button : JButton = new JButton("I'm a AsWing button!");
			pane.append(button);
			pane.append(label);
			pane.setBorder(new EmptyBorder(null, new Insets(10,5,10,5)));
			
			{	// listen to button's click
				var _ : Function = function (e : Event) : void
				{
					++numClicks;
					label.setText(labelPrefix + numClicks);
					label.revalidate();
				}
				button.addEventListener( MouseEvent.MOUSE_UP, _ );
			}
			
			return pane;
		}

	}
	
}